package com.baiyangtx.lingerweather;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Dialog;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class CityChooseDialog extends Dialog {

	private static final int TEXTSIZE = 20 ;
	private static final int TEXTCOLOR = Color.WHITE ;
	
	
	/*
	 * list of city .
	 * Pair for Pair<name , code>
	 */
	private ArrayList<Pair<String, String>> cities = new ArrayList<Pair<String,String>>() ;
	
	
	private Context context ;
	private String province ;
	private CitySelectedListener listener ;
	private GridView cityView ;
	private Button cancal ;
	
	
	
	
	
	
	
	private final View.OnClickListener cancelClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			dismiss() ;
		}
	};
	
	
	private final AdapterView.OnItemClickListener cityChoose = new AdapterView.OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			@SuppressWarnings("unchecked")
			Pair<String, String> city = (Pair<String, String>) parent.getItemAtPosition(position) ;
			listener.onCitySelected(city.first, city.second) ;
		}
		
	} ;
	
	
	private class CityViewAdapt extends BaseAdapter{

		private static final long baseId = 0xFFFF0000 ;
		
		@Override
		public int getCount() {
			return cities.size() ;
		}

		@Override
		public Object getItem(int position) {
			return cities.get(position) ;
		}

		@Override
		public long getItemId(int position) {
			return baseId + position ;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if( convertView != null ){
				return convertView ;
			}
			TextView textView = new TextView(context) ;
			String name = cities.get(position).first ;
			textView.setText(name) ;
			textView.setTextSize(TEXTSIZE) ;
			textView.setTextColor(TEXTCOLOR) ;
			return textView ;
		}
		
	}
	
	
	public CityChooseDialog(Context context , String province , CitySelectedListener listener) {
		super(context) ;
		this.context = context ;
		this.province = province ;
		this.listener = listener ;
	}
	
	
	
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature( Window.FEATURE_NO_TITLE ) ;
		try {
			parserProvince() ;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		setContentView(R.layout.city_dialog) ;
		cancal = (Button)findViewById(R.id.btn_cancal);
		cancal.setOnClickListener(cancelClick) ;
		
		cityView = (GridView)findViewById(R.id.gridview_choosecity) ;
		cityView.setOnItemClickListener(cityChoose) ;
		cityView.setAdapter(new CityViewAdapt()) ;
	}
	
	
	
	
	private void parserProvince() throws XmlPullParserException, IOException{
		XmlResourceParser parser =  context.getResources().getXml(R.xml.city_code) ;
		final String PROVINCE = "province" ;
		final String CITY = "city" ;
		
		boolean inProvince = false ;
		String tagName = null ;
		int event = parser.getEventType() ;
		
		while( event != XmlResourceParser.END_DOCUMENT ){
			tagName = parser.getName() ;
			switch(event){
			case XmlResourceParser.START_TAG :
				if( PROVINCE.equals(tagName) ){
					String name = parser.getAttributeValue(0) ;
					if( province.equals(name) ){
						inProvince = true ;
					}
				}else if( inProvince && tagName.equals(CITY)) {
					String name = parser.getAttributeValue(0) ;
					String code = parser.nextText() ;
					Pair<String, String> city = new Pair<String, String>( name , code ) ;
					cities.add(city) ;
				}
				break ;
				
			case XmlResourceParser.END_TAG :
				if( PROVINCE.equals(tagName) && inProvince ){
					inProvince = false ;
					parser.close() ;
					return ;
				}
			}
			event = parser.next() ;
		}
		parser.close() ;
	}
}





















